package com.ruoyi.web.controller.system.as;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.as.AsUsercrop;
import com.ruoyi.system.service.as.IAsUsercropService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * usercropController
 *
 * @author ruoyi
 * @date 2021-08-07
 */
@RestController
@RequestMapping("/system/usercrop")
public class AsUsercropController extends BaseController
{
    @Autowired
    private IAsUsercropService asUsercropService;

    /**
     * 查询usercrop列表
     */
    @PreAuthorize("@ss.hasPermi('system:usercrop:list')")
    @GetMapping("/list")
    public TableDataInfo list(AsUsercrop asUsercrop)
    {
        startPage();
        List<AsUsercrop> list = asUsercropService.selectAsUsercropList(asUsercrop);
        return getDataTable(list);
    }

    /**
     * 导出usercrop列表
     */
    @PreAuthorize("@ss.hasPermi('system:usercrop:export')")
    @Log(title = "usercrop", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AsUsercrop asUsercrop)
    {
        List<AsUsercrop> list = asUsercropService.selectAsUsercropList(asUsercrop);
        ExcelUtil<AsUsercrop> util = new ExcelUtil<AsUsercrop>(AsUsercrop.class);
        return util.exportExcel(list, "usercrop数据");
    }

    /**
     * 获取usercrop详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:usercrop:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return AjaxResult.success(asUsercropService.selectAsUsercropById(userId));
    }

    /**
     * 新增usercrop
     */
    @PreAuthorize("@ss.hasPermi('system:usercrop:add')")
    @Log(title = "usercrop", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AsUsercrop asUsercrop) {
        int row=1;
        try{
        Long a[]=asUsercrop.getCropTypeIds();
        for (int i = 0; i < a.length; i++) {
            asUsercrop.setCropTypeId(a[i]);
           row= asUsercropService.insertAsUsercrop(asUsercrop);
        }}catch(Exception e) {
        e.printStackTrace();
        }
        return toAjax(row);
    }

    /**
     * 修改usercrop
     */
    @PreAuthorize("@ss.hasPermi('system:usercrop:edit')")
    @Log(title = "usercrop", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AsUsercrop asUsercrop)
    {
        Long userIds[]=new Long[1];
        userIds[0]=asUsercrop.getUserId();
        int row=1;
        asUsercropService.deleteAsUsercropByIds(userIds);


            Long a[]=asUsercrop.getCropTypeIds();
            for (int i = 0; i < a.length; i++) {
                asUsercrop.setCropTypeId(a[i]);
                try{
              row=  asUsercropService.updateAsUsercrop(asUsercrop);
            }catch(Exception e) {
        e.printStackTrace();
    }}
        return toAjax(row);

    }

    /**
     * 删除usercrop
     */
    @PreAuthorize("@ss.hasPermi('system:usercrop:remove')")
    @Log(title = "usercrop", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        try {
            return AjaxResult.success(asUsercropService.deleteAsUsercropByIds(userIds));
        }catch(Exception e){
            e.printStackTrace();
            return toAjax(1);
        }
    }
}
